﻿/* 
 * author: www.cnblogs.com/luminji
 * date  : 2011/12/28
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Luminji.Core.Data;
using Luminji.Exam.Model.PaperTemplates;
using Luminji.Infrastructure.DomainBase;
using Luminji.Infrastructure.UnitOfWork;

namespace Luminji.Exam.Infrastructure.Repositories.PaperTemplates
{
    public class PaperTemplateRepository : SqlServerRepositoryBase<PaperTemplate>, IPaperTemplateRepository
    {
        protected PaperTemplateRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            
        }

        /// <summary>
        /// 获取全部的试卷模版及结构，对结果进行Group，就可以得到全部试卷模版
        /// </summary>
        public IList<PaperTemplate> GetAllPaperTemplatesWithParts()
        {
            //--获取所有的试卷模版结构
            string sql =
                "SELECT a.PaperTemplatePartName, c.PaperTemplateName,  * FROM dbo.PaperTemplatePart a LEFT JOIN QuestionUnit b ON a.QuestionUnitCode = b.QuestionUnitCode LEFT JOIN dbo.PaperTemplate c ON a.PaperTemplateCode = c.PaperTemplateCode ORDER BY c.PaperTemplateCode, OrderValue";
            IEnumerable<PaperTemplate> pts = null;
            using (DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.Text, sql))
            {
                var all = ds.Tables[0].AsEnumerable();
                pts = (from p in all
                       select new PaperTemplate()
                       {
                           PaperTemplateName = p.Field<string>("PaperTemplateName"),
                           PaperTemplateCode = p.Field<string>("PaperTemplateCode")
                       }).Distinct();

                foreach (var paperTemplate in pts)
                {
                    var paperTemplatePart = (from p in all
                                             where p.Field<string>("PaperTemplateCode") == paperTemplate.PaperTemplateCode
                                             select new PaperTemplatePart { PaperTemplateCode = p.Field<string>("PaperTemplateCode"), PaperTemplatePartCode = p.Field<string>("PaperTemplatePartCode"), PaperTemplatePartName = p.Field<string>("PaperTemplatePartName") }).ToList();
                    paperTemplate.PaperTemplateParts = paperTemplatePart as IEnumerable<PaperTemplatePart>;
                }
            }
            return null;
        }

        IList<PaperTemplate> IPaperTemplateRepository.GetAllPaperTemplatesWithParts()
        {
            throw new NotImplementedException();
        }

        public override IList<PaperTemplate> Find()
        {
            throw new NotImplementedException();
        }

        public override PaperTemplate Find(object key)
        {
            throw new NotImplementedException();
        }

        public override void Add(PaperTemplate item)
        {
            throw new NotImplementedException();
        }

        public override void Remove(PaperTemplate item)
        {
            throw new NotImplementedException();
        }
    }
}
